﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace printer
{
   
   public class Employee : person
    {
        public double Ssn;
        public double Salary;
        public string User_Name;
        private string _PassWord;
        public DateTime Work_Start_Time;
        public string Work_State;
        public Employee()
        {
            DataBase.connect("Studio_Service_Company");
        }
       // private string  _Employee_type;
        //public double Ssn
        //{
        //    set { this._Ssn = value; }
        //    get { return this._Ssn; }
        //}

        //public double Salary
        //{
        //    set { this._Salary = value; }
        //    get { return this._Salary; }
        //}

        //public string User_Name
        //{
        //    set { this._User_Name = value; }
        //    get { return this._User_Name; }
        //}


        public string PassWord
        {
            set { this._PassWord = value; }
            get { return this._PassWord; }
        }
        //public DateTime Work_Start_Time
        //{
        //    set { this._Work_Start_Time = value; }
        //    get { return this._Work_Start_Time; }
        //}
        //public string Work_State
        //{
        //    set { this._Work_State = value; }
        //    get { return this._Work_State; }
        //}
        
        public bool login(ref Employee emp)  //ayman sayed
        {
            if (emp.User_Name == "" && emp._PassWord == "")
                return false;
            SqlDataReader reader = DataBase.select_from_database("select * from Employee  where User_Name='" + emp.User_Name + "' AND PassWord='" + emp._PassWord + "'");
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    emp.ID = Convert.ToInt32(reader["Employee_Id"]);
                    emp.Salary = Convert.ToDouble(reader["Salary"]);
                    emp.Ssn = Convert.ToInt32(reader["Ssn"]);
                    emp.Work_State = reader["Work_State"].ToString();
                }
                
               // reader = DataBase.select_from_database("select pk.Kind from Person p,Person_Kind pk where p.Kind_Id=pk.Id and p.Id=" + emp.ID + "");
                //reader = DataBase.select_from_database("select pk.Kind from Person p,Person_Kind pk where p.Kind_Id=pk.Id and p.Id=" + emp.ID + "");
                //while (reader.Read())
                //{
                //    emp.Eployee_Type = (reader["Kind"]).ToString();
                //}
              //  DataBase.connect("Studio_Service_Company");
                
                reader = DataBase.select_from_database("select * from Person where Id='" + emp.ID + "'");
                while (reader.Read())
                {
                    emp.Name = (reader["Name"]).ToString();
                    emp.phone = (reader["Phone"]).ToString();
                    emp.Addres = (reader["Address"]).ToString();
                    emp.Person_type_id = Convert.ToInt32((reader["Kind_Id"]));
                }
                if (emp.Work_State == "active")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
         
        }

        public bool Link_Studio_With_owner(int studio_id, int owner_id)  //ahmed adel
        {
             SqlDataReader reader1 = DataBase.select_from_database("select * from Person  where Id='" + studio_id + "' ");
             SqlDataReader reader2 = DataBase.select_from_database("select * from Person  where Id='" + owner_id + "' ");
             if (!reader1.HasRows&&!reader2.HasRows)
                 return false;
            string Query = "insert into Studio_Owner_Ids (Owner_Id,Studio_Id) values('" + owner_id + "','" + studio_id + "')";
            DataBase.insert_update_delete_in_database(Query);
            return true;

        }
        /// <summary>
        /// add the information fo the person and return with the id of the person
        /// </summary>
        /// <param name="person"></param>
        /// <param name="kind_id"></param>
        /// <returns></returns>
        public int AddPersonDB(person person)   //ahmed adel
        {
            if (person.Name == "" && person.phone == "" && person.Addres == "")
                return -1;
            string Query = "insert into Person (Name,Phone,Address,Kind_Id) values('" + person.Name + "','" + person.phone + "','" + person.Addres + "','" + person.Person_type_id + "')";
           int row= DataBase.insert_update_delete_in_database(Query);
           if (row == 0)
               return -1;
            SqlDataReader reader = DataBase.select_from_database("SELECT TOP 1 Id FROM Person ORDER BY Id DESC");
            if (!reader.HasRows)
                return -1;
            int Id_owner = 0;
            while (reader.Read())
            {
                Id_owner = Convert.ToInt32(reader["Id"]);

            }
            return Id_owner;
            //DataBase.close_connection();
        }
       
        /// <summary>
        /// take the name of the person and return the id of the person
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int get_person_id(string name) //ayman sayed
        {
            SqlDataReader reader = DataBase.select_from_database("SELECT Id FROM Person where Name='" + name + "'");
            if (!reader.HasRows)
                return -1;
             int Id_owner = 0;
            while (reader.Read())
            {
                Id_owner = Convert.ToInt32(reader["Id"]);

            }
            return Id_owner;
        }
        /// <summary>
        /// take the id of the kind that you want to retrieve it from he data base
        /// and it return with list of names that you want
        /// </summary>
        /// <param name="kind_type_id"></param>
        /// <returns></returns>
        
        public int Delete_studio(int studio_Id)
        {
            int owner_id=0;
            string Query = "select Owner_Id from Studio_Owner_Ids where Studio_Id=" + studio_Id + "";
            SqlDataReader reader = DataBase.select_from_database(Query);
            if (!reader.HasRows)
                return -1;
            while (reader.Read())
            {
                owner_id = Convert.ToInt32(reader["Owner_Id"]);
            }
            Query = "select * from Studio_Owner_Ids where Studio_Id=" + owner_id + "";
            reader = DataBase.select_from_database(Query);
            if (!reader.HasRows)
                return -1;
            Query = "delete from Person where Id=" + studio_Id + "";
          int row=DataBase.insert_update_delete_in_database(Query);
          if (row != 1)
              return -1;
            if (reader.Depth > 1)
            {
                return -1;
            }
            else
            {
                return owner_id;
            }
        }
        public bool delete_studio_owner(int owner_id)      //change return bool
        {
           string Query = "delete from Person where Id=" + owner_id + "";
            int row=DataBase.insert_update_delete_in_database(Query);
            if (row != 1)
                return false;
            else
                return true;

        }
        public bool change_password(int id,string current_password,string new_password)
        {
            string pass="";
            string Query = "select PassWord from Employee  where Employee_Id=" + id + "";
            SqlDataReader reader = DataBase.select_from_database(Query);
            while (reader.Read())
            {
                pass = reader["PassWord"].ToString();
            }
            if (pass == current_password)
            {
                Query = "update Employee set PassWord='" + new_password + "' where Employee_Id=" + id + "";
                DataBase.insert_update_delete_in_database(Query);
                return true;
            }
            else
                return false;
        }

       
    }
}
